package com.interfaz.tabla.jcb;

import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Vector;

import javax.swing.AbstractCellEditor;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.controlador.ElementoController;
import com.help.MarcaRenderer;
import com.modelo.Categoria_Elemento;
import com.modelo.Marca;

public class JCMarcaEditor extends AbstractCellEditor implements TableCellEditor{
	
	JComboBox jcbMar;
	
	Object valorActual;
	Marca marca = null;
	ElementoController elemento_controller;
	Vector<Marca> vector_marca;
	
	public JCMarcaEditor(){
		System.out.println("JCMarcaEditor() constructor");
		elemento_controller = new ClassPathXmlApplicationContext("applicationContext.xml").getBean("elementoService",ElementoController.class);
		vector_marca = new Vector<Marca>();
		vector_marca = elemento_controller.llenarMarcaElementoNuevo();
		if(vector_marca!=null){
			jcbMar = new JComboBox(vector_marca);
			jcbMar.setRenderer(new MarcaRenderer());
			ItemListener itemL = new ItemListener(){
				public void itemStateChanged(ItemEvent ie){
					if(ie.getStateChange()==ItemEvent.SELECTED){
						
						//ItemSelectable itemS = ie.getItem();
						Marca el_mar = (Marca)ie.getItem();
						System.out.println("JCMarcaEditor: "+el_mar.getNombre());
						valorActual = el_mar;			
						
					}
				}
			};
			jcbMar.addItemListener(itemL);
		}
	}
		
	
	@Override
	public Object getCellEditorValue() {
		// TODO Auto-generated method stub
		
		return valorActual;
	}

	@Override
	public Component getTableCellEditorComponent(JTable arg0, Object arg1,
			boolean arg2, int arg3, int arg4) {
		// TODO Auto-generated method stub
		return jcbMar;
	}
	
}
